
/**
 * 
 * @param win
 * @returns {Reload}
 */
function Reload(win){
	if(!win){
		win = window;
	}
	this.win_ = win;
	this.params_ = {};
	var locationHref = this.win_.location.href;
	var paramsStr = "";
	var wIndex = locationHref.indexOf("\?");
	if(wIndex>0){
		var jIndex = locationHref.indexOf("\#");
		if(jIndex>0){
			paramsStr = locationHref.substring(wIndex+1,jIndex);
		}
		else{
			paramsStr = locationHref.substring(wIndex+1);
		}
		paramsStr = '{"'+paramsStr.replace(/\&/g,'","').replace(/\=/g,'":"')+'"}';
		eval("this.params_="+paramsStr);
	}
}
Reload.prototype.getParams = function (){
	return this.params;
};
Reload.prototype.addParam = function (key, value){
	eval("this.params_['"+key+"']=encodeURIComponent('"+value+"')");
};
Reload.prototype.removeParam = function (key){
	eval("this.params_['"+key+"']= undefined");
};
Reload.prototype.getParam = function (key){
	var param = decodeURIComponent(this.params_[key]);
	if (param == 'undefined') {
		param = null;
	}
	return param;
};
Reload.prototype.reload = function (){
	var vars = "";
	for(var key in this.params_){
		if(undefined!=this.params_[key]){
			vars+=(vars==""?"":"&")+key+"="+this.params_[key];
		}
	}
	var href = this.win_.location.href;
	if(href.indexOf("\?")>0){
		href = href.substring(0,href.indexOf("\?"));
	}
	else if(href.indexOf("\#")>0){
		href = href.substring(0,href.indexOf("\#"));
	}
	if(vars){
		this.win_.location.href = href+"?"+vars;
	}
	else{
		this.win_.location.href = href;
	}
	
};

var pageReload = new  Reload();
$(document).ready(function() {
	//mo_yq5	注册事件：点击分页
	$("#pageLink a[page]").click(function(){
			pageReload.addParam("np", $(this).attr("page"));
			pageReload.reload();
		});
	$("#goToBt").click(function() {
		var nowPage = parseInt($("#goToPage").val());
		if (nowPage > 0) {
			pageReload.addParam("np", nowPage);
			pageReload.reload();
		}
	});
});


/**
 * jquery扩展：分页生成函数
 * @author mo_yq5
 */
(function(){
	$.extend($.fn,{
		pageLink: function(pageInfo, callback) {
			$that = $(this);

			var pageLink = "<ul id='pageLink'>";
			if (pageInfo.nowPage > 1) {
				pageLink += "<li class='prv'><a page='" + (pageInfo.nowPage - 1) + "' title='上一页' href='javascript:void(0);'><i>上一页</i></a></li> ";
	
			} else {
				pageLink += "<li class='prv'><font><i>上一页</i></font></li> ";
			}
			if (pageInfo.totalPage >= pageInfo.linkCount) {
				var pageNum = Math.floor(pageInfo.nowPage-pageInfo.linkCount/2);
				if(pageNum + pageInfo.linkCount > pageInfo.totalPage){
					pageNum = pageInfo.totalPage - pageInfo.linkCount + 1;
				}
				if (pageNum > 1) {
					pageLink += "<li><a page='1' href='javascript:void(0);'><i>1</i></a></li> ";
				}
				if( pageNum > 2){
					pageLink += "<li><em>...</em></li> ";
				}
				if( pageNum <= 0){
					pageNum=1;
				}
				for (var i=1; i<=pageInfo.linkCount; i++) {
					if (pageNum <= pageInfo.totalPage) {
						if (pageInfo.nowPage == pageNum) {
							pageLink +="<li><span><i>" + pageNum + "</i></span></li> ";
						}
						else {
							pageLink +="<li><a page='" + pageNum + "' href='javascript:void(0);'><i>" + pageNum + "</i></a></li> ";
						}
					}
					pageNum++;
				}
				if (pageNum < pageInfo.totalPage) {
					pageLink += "<li><em>...</em></li> <li><a page='" + pageInfo.totalPage + "' href='javascript:void(0);'><i>" + pageInfo.totalPage + "</i></a></li> ";
				}
				else if( pageNum == pageInfo.totalPage) {
					pageLink += "<li><a page='" + pageInfo.totalPage + "' href='javascript:void(0);'><i>" + pageInfo.totalPage + "</i></a></li> ";
				}
			}
			else {
				for (var i=1; i <= pageInfo.totalPage; i++) {
					if (pageInfo.nowPage <= pageInfo.totalPage) {
						if (pageInfo.nowPage == i) {
							pageLink += "<li><span><i>" + i + "</i></span></li>";
						}
						else{
							pageLink += "<li><a page='" + i + "' href='javascript:void(0);'><i>"+i+"</i></a></li> ";
						}
					}
				}
			}			
			if (pageInfo.nowPage < pageInfo.totalPage) {
				pageLink += "<li class='next'><a page='" + (pageInfo.nowPage + 1) + "' title='下一页' href='javascript:void(0);' ><i>下一页</i></a></li>";
			}
			else{
				pageLink += "<li class='next'><font><i>下一页</i></font></li>";
			}
			pageLink += "<li class='turn'><font>跳至</font><input type='text' id='goToPage' value='" + pageInfo.nowPage + "'/><font>页</font></li>";
			pageLink += "<li class='btn'><input type='button' id='goToBt' value='确定' /></li>";
			pageLink += "</ul>";
			
			$that.html(pageLink);
			
			$that.find("a[page!='0']").click( function() {
				callback($(this).attr("page"));
			});
			$that.find("#goToBt").click(function() {
				var destPage = parseInt($that.find("#goToPage").val());
				if (destPage > 0 && destPage <= pageInfo.totalPage) {
					callback(destPage);
				}
			});
		}
	});
})();